﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using Database;

namespace Database
{
   public class CT_DonDatHang
    {
        private int maddh;

        public int Maddh
        {
            get { return maddh; }
            set { maddh = value; }
        }
        private int mamay;

        public int Mamay
        {
            get { return mamay; }
            set { mamay = value; }
        }
        private int soluong;

        public int Soluong
        {
            get { return soluong; }
            set { soluong = value; }
        }
        private double giaban;

        public double Giaban
        {
            get { return giaban; }
            set { giaban = value; }
        }
        public CT_DonDatHang()
        {
        }
        public CT_DonDatHang(int maddh, int mamay, int soluong, float giaban)
        {
            this.maddh = maddh;
            this.mamay = mamay;
            this.soluong = soluong;
            this.giaban = giaban;
        }
        public static DataSet DSCTDDH( int maddh)
        {
            DataSet ds = new DataSet();
            string query = "select * from ct_dondathang";
            sqlConnection db = new sqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }
        public static DataSet CTDDHTheoMa(int maddh)
        {
            DataSet ds = new DataSet();
            string query = "select * from ct_dondathang where maddh="+maddh;
            sqlConnection db = new sqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }       
        public bool ThemCTDDH(int maddh,int mamay,int soluong, float giaban)
        {
            bool kq = true;

            try
            {
                string query = "insert into ct_dondathang(maddh,mamay,soluong,giaban) values('{0}','{1}','{2}','{3}')";
                query = String.Format(query,maddh,mamay,soluong,giaban);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool CapNhatCTDDH(int maddh,int mamay,int soluong,float giaban)
        {
            bool kq;
            SqlCommand cmd;
            sqlConnection db = new sqlConnection();
            try
            {
                string query = "update ct_dondathang set soluong=@soluong,giaban=@giaban where maddh=@maddh and mamay=@mamay ";
                cmd = new SqlCommand(query, db.GetConnect());
                cmd.Parameters.Add("@soluong", SqlDbType.Int).Value = soluong;
                cmd.Parameters.Add("@giaban", SqlDbType.Float).Value = giaban;
                cmd.Parameters.Add("@maddh", SqlDbType.Int).Value = maddh;
                cmd.Parameters.Add("@mamay", SqlDbType.Int).Value = mamay;
                cmd.ExecuteNonQuery();
                kq = true;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool XoaCTDDH(int maddh,int mamay)
        {
            bool kq = true;

            try
            {
                string query = "Delete ct_dondathang where maddh='{0}' and mamay='{1}'";
                query = String.Format(query,maddh,mamay);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                cmd.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool XoaCTDDHTheoMa(int maddh)
        {
            bool kq = true;

            try
            {
                string query = "Delete ct_dondathang where maddh='{0}'";
                query = String.Format(query, maddh);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                cmd.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public CT_DonDatHang ThongTinCTDDH(int maddh)
        {
            string query = "select * from ct_dondathang where maddh="+maddh;
            sqlConnection db = new sqlConnection();
            SqlCommand sc = new SqlCommand(query, db.GetConnect());
            SqlDataReader reader = sc.ExecuteReader();
            CT_DonDatHang ctddh = new CT_DonDatHang();
            while (reader.Read())
            {
                ctddh.maddh = reader.GetInt32(0);
                ctddh.mamay = reader.GetInt32(1);
                ctddh.soluong = reader.GetInt32(2);
                ctddh.giaban = reader.GetDouble(3);                
            }
            return ctddh;
        }
        public void ThemCT_DonDatHang(CT_DonDatHang add)
        {

            SqlCommand cmd;
            try
            {
                sqlConnection db = new sqlConnection();
                cmd = new SqlCommand("insert into CT_DonDatHang(maddh,mamay,soluong,giaban) values(@maddh,@mamay,@soluong,@giaban)", db.GetConnect());
                cmd.Parameters.Add("@maddh", SqlDbType.Int).Value = add.Maddh;
                cmd.Parameters.Add("@mamay", SqlDbType.Int).Value = add.Mamay;
                cmd.Parameters.Add("@soluong", SqlDbType.Int).Value = add.soluong;
                cmd.Parameters.Add("@giaban", SqlDbType.Float).Value = add.giaban;

                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

    }
}
